import java.io.*;
import java.util.*;

public class httpresponse {
	private Hashtable<String, String> headers;
	private OutputStream out;

	public httpresponse(OutputStream out) {
		this.out = out;
		headers = new Hashtable<String, String>();
	}

	public OutputStream getOutputStream() {
		return out;
	}

	public void addHeader(String key, String value) {
		if (key != null && value != null) {
			headers.put(key, value);
		}
	}

	public String getStatusText(int status) {
		String txt;
		switch (status) {
		case 200:
			txt = "OK";
			break;
		case 400:
			txt = "Bad Request";
			break;
		case 404:
			txt = "Not Found";
			break;
		case 500:
			txt = "Internal Server Error";
			break;
		case 501:
			txt = "Not Implemented";
			break;
		default:
			txt = "";
		}
		return txt;
	}

	public void printStatus(int status) throws IOException {
		String line = "HTTP/1.0 " + status + " " + getStatusText(status)
				+ "\r\n";
		out.write(line.getBytes("ISO-8859-1"));
		out.flush();
	}

	public void printHeaders() throws IOException {
		Enumeration<String> e = headers.keys();
		while (e.hasMoreElements()) {
			String key = e.nextElement();
			String line = key + ": " + headers.get(key) + "\r\n";
			out.write(line.getBytes("ISO-8859-1"));
		}
		String line = "\r\n";
		out.write(line.getBytes("ISO-8859-1"));
		out.flush();
	}
}